home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / applic / ntp / acts.zoo / rdline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-15  |  8.8 KB  |  313 lines

  1. #include "nbstime.h"
  2. /*
  3.         following is the block of global variables
  4.         used by all of the subroutines
  5.         these variables are defined here with their default
  6.         values which are modified by reading a configuration
  7.         file named settim.cfg.  this is done by subroutine setcfg.
  8.  
  9.     the global variables depend on which version this is.  the
  10.     ibm-pc version converts from received UTC to local time 
  11.     based on the values in the configuration file, while UNIX
  12.     versions store the time in UTC
  13. */
  14. #include <stdio.h>
  15. #ifdef IBMPC
  16. int utcdif = -7;   /* local standard time - utc in hours */
  17. int dsflag = 1;    /* daylight savings time? 1 = yes, 0 = no */
  18. #endif
  19. int cmport = 0;    /* 0 for com1 port, 1 for com2 port, etc. */
  20. char number[30] = { 'A','T','D','T','9',',','4','9','4','4','7','7','4',
  21.                    '\r', 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
  22.                      0 , 0 , 0 , 0 };  /* telephone number to dial */
  23. #ifdef IBMPC
  24. int atflag = 1;    /* AT-type machine? 1=yes, 0=no */
  25. #endif
  26. int debug  = 0;    /* list diagnostic messages? 1=yes, 0=no */
  27. int echo   = 1;    /* expect echoes from modem? 1=yes, 0=no  */
  28. int hs     = 1;    /* 1200 baud dialing? 1=yes, 0=no, use 300*/
  29. #ifdef IBMPC
  30. int lpt    = 0;    /* pulse lpt1 on time? 1=yes, 0=no */
  31. int lpadr  = 0x37a; /* default command register for line printer */
  32. #ifndef BIOS
  33. int cmadr  = 0; /* command register for COM port, NO DEFAULT !!  */
  34. #endif
  35. #endif
  36. int setclk   = 1;     /* 1=set clock, 0=check time only          */
  37. int wrtdif   = 0;     /* 1=archive time dif, 0= do not 2=get rate too  */
  38. FILE *jop;            /* file handle for writing difference */
  39. /* 
  40.     following structure is used to read
  41.     previous time difference from file if
  42.     "A" mode is selected
  43.  
  44.     structure contains time of previous
  45.     difference (year, month, day, hour,
  46.     minute and second) followed by difference
  47.     and units of difference.  note that the
  48.     IBMPC version has two such pairs -- one for
  49.     the RAM clock and one for the CMOS clock
  50. */
  51. struct tmprev
  52.     {
  53.     int yrprev;
  54.     int moprev;
  55.     int dyprev;
  56.     int hrprev;
  57.     int mnprev;
  58.     int scprev;
  59.     float dffprv;
  60.     char unprev;
  61. #ifdef IBMPC
  62.     float datprv;
  63.     char uatprv;
  64. #endif
  65. } tmpp;
  66. main(argc,argv)
  67. int argc;
  68. char *argv[];
  69. {
  70. /*
  71.         this is the main program named rdline for task nbstime.
  72. */
  73. #ifdef IBMPC
  74. #include <dos.h>
  75. #endif
  76. char buf[280],ichar;
  77. void wrtbuf(),inilin(),hangup(),sndptr(),diftim(),arcdif();
  78. int rdbuf(),cmplst();
  79. void wait(),parset(),setcfg();
  80. int dial();
  81. int j,param;
  82. int len;
  83. int lim;
  84. int tmo  = 0;
  85. char ie1 = '*';
  86. char ie2 = '#';
  87. char ie3 = -120;
  88. char ie3a= 'M';
  89. int jm;
  90. /*
  91.         following constant sets timeout value for read.
  92.         since BIOS commands are slower, time-out value is less
  93. */
  94. #ifdef IBMPC
  95. #ifdef BIOS
  96. int ttmo = -6;
  97. #endif
  98. #ifndef BIOS
  99. int ttmo = -15;
  100. #endif
  101. #endif
  102. #ifdef SUN
  103. int ttmo = -20;
  104. #endif
  105. /*
  106.         following variables are used in cmplst and are defined
  107.         here so that they remain defined across consecutive
  108.         calls.
  109.         osec is the previous value of the second
  110.         obuf holds the time string of the previous transmission
  111.         count is 0 until a line has been read
  112. */
  113. int count = 0;
  114. int osec  = 0;
  115. char obuf[25];
  116. /*
  117.         see if there is a switch on the command line
  118. */
  119.         if( (argc > 1) && (*argv[1] == '-') )
  120.         {
  121.         argv[1]++;
  122.         if(*argv[1] == 'd') debug = 1;
  123.         }
  124. #ifdef IBMPC
  125.         printf("\nVersion of %s \n");
  126. #endif
  127. #ifdef SUN
  128.     printf("\n Version for SUN system \n");
  129. #endif
  130.         
  131. /*
  132.         open configuration file and set global parameters
  133. */
  134.         setcfg();
  135. /*
  136.         following code sets number of times first loop is done.
  137.         at 300 baud, the limit is inifinte since the time is neither
  138.         set nor checked at that lower speed.
  139. */
  140.         if(hs == 1) lim=12;
  141.         else        lim=9999;
  142.         if(debug != 0)
  143.         {
  144.         printf("\n input configuration \n %s",number);
  145. #ifdef IBMPC
  146.         printf("\n utcdif= %d",utcdif);
  147.         printf("\n cmport= COM%1d",cmport+1);
  148.         #ifdef BIOS
  149.         printf("\n i/o via calls to BIOS routines.");
  150.     #endif
  151.         #ifndef BIOS
  152.         printf("\n i/o via direct operations on port.");
  153.         #endif
  154.         printf("\n atflag= %d",atflag);
  155.         printf("\n dsflag= %d",dsflag);
  156. #endif
  157. #ifdef SUN
  158.     printf("\n port file descriptor=%d",cmport);
  159. #endif
  160.         printf("\n echo  = %d",echo);
  161.         printf("\n hs    = %d",hs);
  162. #ifdef IBMPC
  163.         printf("\n lpt1  = %d",lpt);
  164.         printf("\n lp adr= %x",lpadr);
  165. #endif
  166.         printf("\n set clk ?, (1=y)= %d",setclk);
  167.         printf("\n wrt to file (1=y), (2=y & rate)=%d",wrtdif);
  168.         }
  169.         if( (setclk == 0) || (hs == 0) )
  170.               printf("\n Computer clock will not be set.");
  171. /*
  172.         initialize com line
  173. */
  174.         inilin();
  175.     if(debug != 0) printf("\n initialization complete.");
  176. /*
  177.         if debug is turned on and if direct calls are used, print
  178.         effective address which was determined by inilin
  179. */
  180. #ifdef IBMPC
  181. #ifndef BIOS
  182.         if(debug != 0) printf("\n hex. address of port=%x",cmadr);
  183. #endif  
  184. #endif
  185. /*
  186.         now dial number -- continue if dial status shows connect
  187. */
  188.         if(dial() == 1)
  189.         {
  190. /*
  191.         now start reading from line
  192.         note that 6 lines will be read at 1200 baud followed by a
  193.         time-set. at 300 baud lim is essentially infinite so that
  194.         this loop continues until it is ended by a timeout caused when
  195.         the NBS folks hang up.
  196. */
  197.         for(j=0; j<lim; j++)
  198.            {
  199.            len=rdbuf(buf,ie1,ie2,ie3a,ttmo);
  200.            if( (len > 0) && (buf[0] > 0) )
  201.               {
  202. #ifdef IBMPC
  203.               if(lpt != 0) sndptr();  /* pulse strobe of lpt1*/
  204. #endif
  205.               wrtbuf(&buf[len-1]);    /* echo terminating character */
  206. #ifdef IBMPC
  207.               printf("\n %s",buf);
  208. #endif
  209. #ifdef SUN
  210.           printf("%s",buf);       /* SUN version uses no newline*/
  211. #endif    
  212.               }
  213.            else
  214.               {
  215.               tmo=1;     /* show exit on time out */
  216.               break;     /* get out of loop on a time-out*/
  217.               }
  218.            }
  219. /*
  220.         write difference to archive file if enabled
  221.         and if the previous loop did not end on a time-out.
  222.         if it did end on a time-out then skip this part since
  223.         the connection has been lost
  224. */
  225.         if (tmo == 0)
  226.         {
  227.            len=rdbuf(buf,ie1,ie2,ie3,-3);
  228.            if( (len > 0) && (buf[0] > 0) )
  229.               {
  230. #ifdef IBMPC
  231.               if(lpt != 0) sndptr();
  232. #endif
  233.               wrtbuf(&buf[len-1]);
  234.               if(wrtdif !=0) arcdif(buf);
  235.               }
  236.            else
  237.               {
  238.               tmo = 1;
  239.               }
  240.         }
  241. /*
  242.         now read next two lines and be sure they are
  243.         sequential.  If they are, parse second line and set time
  244.         if enabled. if sequential comparison fails, print message
  245.         in comparison subroutine and try again.
  246.  
  247.         inner while loop continues until two consecutive lines pass
  248.         the test or until the read fails on a time-out and tmo =1.
  249. */
  250.         if(tmo == 0)           /* do not do this if timeout detected*/
  251.         {
  252.         do
  253.         {
  254.            len=rdbuf(buf,ie1,ie2,ie3,-3);
  255.            if( (len > 0) && (buf[0] > 0) )
  256.               {
  257. #ifdef IBMPC
  258.               if(lpt != 0) sndptr();
  259. #endif
  260.               wrtbuf(&buf[len-1]);
  261.               jm=cmplst(buf,&count,&osec,obuf);
  262. /*
  263.         if clock set is enabled and if the test for sequential times
  264.         are both ok (both values are 1) then set the clock
  265. */
  266.               if(  (setclk == 1) && (jm == 1) )parset(buf);
  267.               }
  268.            else
  269.               {
  270.               tmo=1;
  271.               }
  272.         } while ( (jm < 1) && (tmo == 0) );
  273.         }   /* end of if tmo == 0 */
  274.  /*
  275.         now read next line and then check the time
  276. */
  277.         if(tmo == 0)
  278.         {
  279.         len=rdbuf(buf,ie1,ie2,ie3,-3);
  280.         if( (len > 0) && (buf[0] > 0) )
  281.            {
  282. #ifdef IBMPC
  283.            if(lpt != 0) sndptr();
  284. #endif
  285.            wrtbuf(&buf[len-1]);
  286.            diftim(buf);
  287.            }
  288.         else
  289.            tmo = 1;
  290.         }
  291. /*
  292.         if lpt is on, then continue reading lines until ended
  293.         by a timeout and if we have not had a timeout yet
  294. */
  295. #ifdef IBMPC
  296.         if( (lpt != 0) && (tmo == 0) )
  297.         {
  298.            do
  299.               {
  300.               len=rdbuf(buf,ie1,ie2,ie3,-3);
  301.               if( (len > 0) && (buf[0] > 0) )
  302.                  {
  303.                  sndptr();
  304.                  wrtbuf(&buf[len-1]);
  305.                  printf("\n %s",buf);
  306.                  }
  307.                } while (len > 0);     /* loop until nothing more read */
  308.         }
  309. #endif
  310.         hangup();
  311.         }
  312. }
  313.